package com.hellpro.lolbox.mine.presenter;

import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioGroup;

import com.hellpro.lolbox.R;
import com.hellpro.lolbox.mine.BmobUser;
import com.hellpro.lolbox.mine.view.IUserInfoActivity;
import com.hellpro.lolbox.utils.ToastShowCenter;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * 类描述:
 * 创建人:阿犇
 * 创建时间:2016/10/24
 * 备注:基本资料的设置
 */

public class UserInfoPresenter{

    private boolean isSex = true;
    private IUserInfoActivity mIUserInfoActivity;
    private PopupWindow popupWindow;

    public UserInfoPresenter(IUserInfoActivity iUserInfoActivity) {
        this.mIUserInfoActivity = iUserInfoActivity;
    }

    /**
     * 类描述:
     * 创建人:阿犇
     * 创建时间:2016/10/24
     * 备注:设置头像，从相机或者相册选择
     */

    public void setUserNameIcon() {
        //TODO:

        LinearLayout view = mIUserInfoActivity.getPopuWindow();

        if (popupWindow == null) {

            popupWindow = new PopupWindow(mIUserInfoActivity.getUserInfoContext());
            popupWindow.setBackgroundDrawable(new BitmapDrawable());

            popupWindow.setFocusable(true); // 设置PopupWindow可获得焦点
            popupWindow.setTouchable(true); // 设置PopupWindow可触摸
            popupWindow.setOutsideTouchable(true); // 设置非PopupWindow区域可触摸
            popupWindow.setContentView(view);

            popupWindow.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
            popupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
//            popupWindow.setAnimationStyle(R.style.popuStyle);   //设置 popupWindow 动画样式
        }
        popupWindow.showAtLocation(mIUserInfoActivity.getUserInfoIvShow(), Gravity.BOTTOM, 0, 0);
        popupWindow.update();
        Button btnBack = (Button) view.findViewById(R.id.popup_btn_back);
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });


    }

    /**
     * 类描述:
     * 创建人:阿犇
     * 创建时间:2016/10/24
     * 备注:上传个人信息到Bmob
     */

    public void setUserInfoToBmob() {

        String birthday = mIUserInfoActivity.getEtUserBirthday().getText().toString();
        String nickName = mIUserInfoActivity.getEtUserNickName().getText().toString();
        String phoneNo = mIUserInfoActivity.getPhoneNo();
        final String password = mIUserInfoActivity.getPassword();
        final BmobUser user = new BmobUser();
        mIUserInfoActivity.getSex().setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.mine_userInfo_woman) {
                    isSex = false;
                }
                user.setSex(isSex);
            }
        });
        //上传数据信息
        user.setUsername(phoneNo);
        user.setPassword(password);
        user.setNickName(nickName);
        user.setBirthday(birthday);
        user.setSex(isSex);
        user.signUp(new SaveListener<BmobUser>() {
            @Override
            public void done(BmobUser user, BmobException e) {
                if (e == null) {
                    ToastShowCenter.centerToast(mIUserInfoActivity.getUserInfoContext(), "信息已保存");
//                    UIManger.startToActivity(mIUserInfoActivity.getUserInfoContext(), MainActivity.class);
                } else {
                    ToastShowCenter.centerToast(mIUserInfoActivity.getUserInfoContext(), "信息保存失败");
                }
            }
        });

    }

}
